﻿using CubeCube.Utils;
using CubeCube.World;
using CubeCube.Object.Component;
using System.Collections.Generic;

namespace CubeCube.Object
{
    public class CCObject
    {
        public int objectId;
        public CubeWorld world;
        public CCObjectDefine definition;
        public Vector postion;
        public Vector rotation;
        public byte energy;
        public bool destroyed;

        private List<ObjectComponent> components;

        public CCObject(int objectId)
        {
            this.objectId = objectId;
        }

        public void AddComponent(ObjectComponent component)
        {
            if (components == null)
                components = new List<ObjectComponent>();

            components.Add(component);

            component.AddedToObject(this);
        }

        public void RemoveComponent(ObjectComponent component)
        {
            components.Remove(component);

            component.RemovedFromObject();
        }

        public virtual void Clear()
        {
            if (components != null)
            {
                for(int i = 0; i < components.Count; ++i)
                {
                    components[i].RemovedFromObject();
                }
                components.Clear();
            }

            world = null;
        }

        public virtual void Update(float deltaTime)
        {
            if (components != null)
            {
                for (int i = 0; i < components.Count; ++i)
                {
                    components[i].Update(deltaTime);
                }
            }
        }

        //TODO: 序列化

    }
}
